import java.util.*;
import java.io.*;
class Student {
    String id;    // 学号
    String name;  // 姓名
    int score;    // 成绩
    public Student(String id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }
    public String getId() {
        return id;
    }
    public String toString() {
        return id + "," + name + "," + score;
    }
}
class StudentFile {
    ArrayList<Student> students = new ArrayList<>();
    private String file;
    public StudentFile(String file) throws IOException {
        this.file = file;
        FromFile();
    }
    private void FromFile() throws IOException {
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String len;
            while ((len = br.readLine()) != null) {
                String[] information = len.split(",");
                String id = information[0];
                String name = information[1];
                int score = Integer.parseInt(information[2]);
                students.add(new Student(id, name, score));
            }
        }
    }
    public void addStudent(Student student) {
        students.add(student);
        System.out.println("学生添加成功");
    }
    public Student findId(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }
    public void saveFile() throws IOException {
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
            for (Student student : students) {
                bw.write(student.toString());
                bw.newLine();
            }
        }
        System.out.println("数据保存成功");
    }
}

public class xitong {
    public static void main(String[] args) throws IOException {
        String file ="C:\\Users\\有理想的咸鱼\\IdeaProjects\\untitled\\students.txt";
        StudentFile system = new StudentFile(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("请选择操作：");
            System.out.println("1. 添加学生");
            System.out.println("2. 按学号查询学生");
            System.out.println("3. 保存数据并退出");
            try {
                int choice = Integer.parseInt(br.readLine());
                switch (choice) {
                    case 1:
                        System.out.println("请输入要添加的学生数量: ");
                        int num = Integer.parseInt(br.readLine());
                        for (int i = 0; i < num; i++) {
                            System.out.println("请输入第 " + (i + 1) + " 个学生的学号: ");
                            String id = br.readLine();
                            System.out.println("请输入第 " + (i + 1) + " 个学生的姓名: ");
                            String name = br.readLine();
                            System.out.println("请输入第 " + (i + 1) + " 个学生的成绩: ");
                            int score = Integer.parseInt(br.readLine());
                            Student student = new Student(id, name, score);
                            system.addStudent(student);
                        }
                        break;
                    case 2:
                        System.out.println("请输入要查询的学生学号: ");
                        String queryId = br.readLine();
                        Student foundStudent = system.findId(queryId);
                        if (foundStudent != null) {
                            System.out.println("找到学生: " + foundStudent);
                        } else {
                            System.out.println("未找到该学生");
                        }
                        break;
                    case 3:
                        system.saveFile();
                        System.out.println("数据已保存，系统退出。");
                        br.close();
                        return;  // 直接退出main方法，结束程序
                    default:
                        System.out.println("无效的选择，请重新输入。");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入格式错误，请输入正确的数字。");
            }
        }
    }
}
